//
//  TakingViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "TakingViewController.h"

@interface TakingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;     //滑动控制器
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;    //最热列表
@property (weak, nonatomic) IBOutlet UITableView *newestTableView; //最新列表
@property (nonatomic,strong)UISegmentedControl *segementControl;   //分段控制器
@property (nonatomic,strong)NSMutableArray *newestArray;           //最新列表数据
@property (nonatomic,strong)NSMutableArray *hotArray;              //最热列表数据
@end

@implementation TakingViewController
- (UISegmentedControl *)segementControl{
    if (!_segementControl) {
        _segementControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
        //插入标签
        [_segementControl insertSegmentWithTitle:@"hot" atIndex:0 animated:YES];
        [_segementControl insertSegmentWithTitle:@"new" atIndex:1 animated:YES];
        [_segementControl addTarget:self action:@selector(clickSegementAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _segementControl;
}
- (NSMutableArray *)newestArray{
    if (!_newestArray) {
        _newestArray = [[NSMutableArray alloc]init];
    }
    return _newestArray;
}
- (NSMutableArray *)hotArray{
    if (!_hotArray) {
        _hotArray = [[NSMutableArray alloc]init];
    }
    return _hotArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;          //防止跑偏
    self.navigationItem.titleView = self.segementControl;  //设置分段控制器位置
    
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TalkingCell" bundle:nil] forCellReuseIdentifier:@"talkModel_cell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"TalkingCell" bundle:nil] forCellReuseIdentifier:@"talkModel_cell"];
    
    
    
    
    [RequestManager fetchDataWithUrl:kTakingUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TalkingModel *model = [[TalkingModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotArray addObject:model];
            NSLog(@"%@",self.hotArray);
        }
        
    } updata:^{
        [self.hotTableView reloadData];
    }];
    
    [RequestManager fetchDataWithUrl:kTakingUrl parameter:@{ @"sort":@"addtime"} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TalkingModel *model = [[TalkingModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.newestArray addObject:model];
            NSLog(@"最新%@",self.newestArray);
        }
        
    } updata:^{
        [self.newestTableView reloadData];
    }];


}
//- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
//    [RequestManager fetchDataWithUrl:kTakingUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {
//        
//        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
//            TalkingModel *model = [[TalkingModel alloc]init];
//            [model setValuesForKeysWithDictionary:smallDic];
//            [self.newestArray addObject:model];
//            NSLog(@"最新%@",self.newestArray);
//        }
//        
//    } updata:^{
//        [self.newestTableView reloadData];
//    }];
//
//}
//- (void)loadhotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
//    [RequestManager fetchDataWithUrl:kTakingUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {
//        
//        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
//            TalkingModel *model = [[TalkingModel alloc]init];
//            [model setValuesForKeysWithDictionary:smallDic];
//            [self.hotArray addObject:model];
//            NSLog(@"%@",self.hotArray);
//        }
//        
//    } updata:^{
//        [self.hotTableView reloadData];
//    }];
//    
//}



//segement点击事件
- (void)clickSegementAction:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}
#pragma mark---------ScrollViewDelegate---------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //判断滚动的是不是要滚动的
    if (scrollView == self.scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.segementControl.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kWidth){
            self.segementControl.selectedSegmentIndex = 1;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.newestTableView) {
        return self.newestArray.count;
    }else{
        return self.hotArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TalkingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"talkModel_cell" forIndexPath:indexPath];
  
    if (tableView == self.newestTableView) {
        [cell setCellWithModel:self.newestArray[indexPath.row]];
    }else{
        [cell setCellWithModel:self.hotArray[indexPath.row]];
        
        
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
